package com.prayerdiary.fragments;

import java.util.Calendar;
import java.util.Date;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.prayerdiary.app.R;
import com.prayerdiary.database.PrayerDiaryDBHelper;
import com.prayerdiary.utils.Utils;

public class AddMonthFragment extends Fragment implements OnClickListener
{
	@Override
	public void onActivityCreated(Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.add_prayer_month, container, false);
		populateSpinners(view);
		Button btn = (Button) view.findViewById(R.id.apm_add_btn);
		Button delMonthBtn = (Button) view.findViewById(R.id.apm_del_btn);
		btn.setOnClickListener(this);
		delMonthBtn.setOnClickListener(this);
		return view;
	}

	private void populateSpinners(View v)
	{
		String monthNames[] = new String[]
		{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
		FragmentActivity activity = getActivity();
		ArrayAdapter<String> monthAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item, monthNames);
		monthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		Spinner monthSpinner = (Spinner) v.findViewById(R.id.aqp_month_spinner);
		monthSpinner.setAdapter(monthAdapter);

		Integer[] years = getYears();
		ArrayAdapter<Integer> yearAdapter = new ArrayAdapter<Integer>(activity, android.R.layout.simple_spinner_item, years);
		yearAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		Spinner yearSpinner = (Spinner) v.findViewById(R.id.aqp_year_spinner);
		yearSpinner.setAdapter(yearAdapter);

		Spinner delMonthSpinner = (Spinner) v.findViewById(R.id.aqp_delmonth_spinner);
		delMonthSpinner.setAdapter(monthAdapter);

		Spinner delYearSpinner = (Spinner) v.findViewById(R.id.aqp_delyear_spinner);
		delYearSpinner.setAdapter(yearAdapter);
	}

	/**
	 * Get 100 years starting from last 99 years
	 */
	private Integer[] getYears()
	{
		Integer[] years = new Integer[100];
		int thisYear = Calendar.getInstance().get(Calendar.YEAR);
		for (int i = 0; i < 100; i++)
			years[i] = thisYear - i;
		return years;
	}

	public void onClick(View v)
	{
		if (v.getId() == R.id.apm_add_btn)
		{
			addPrayerMonth();
		}
		if (v.getId() == R.id.apm_del_btn)
		{
			deletePrayerMonth();
		}
	}

	private void deletePrayerMonth()
	{
		Spinner monthSpinner = (Spinner) getActivity().findViewById(R.id.aqp_delmonth_spinner);
		Spinner yearSpinner = (Spinner) getActivity().findViewById(R.id.aqp_delyear_spinner);

		int month = (Integer) monthSpinner.getSelectedItemPosition();
		int year = (Integer) yearSpinner.getSelectedItem();

		PrayerDiaryDBHelper dbHelper = new PrayerDiaryDBHelper(getActivity());
		if (!dbHelper.isMonthAdded(month, year))
		{
			Utils u = new Utils();
			u.showMessage("Unsuccessful", "Month not added", getActivity()).run();
		}
		else
		{
			int days = calculateDays(month, year);

			Calendar cal = Calendar.getInstance();
			cal.clear();
			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.MONTH, month);
			Date date = null;
			dbHelper.deleteCurrentPrayerMonth(month, year);
			for (int i = 0; i < days; i++)
			{
				cal.set(Calendar.DAY_OF_MONTH, i + 1);
				date = cal.getTime();
				dbHelper.deleteCurrentPrayerRecord(date.getTime());
			}

			CharSequence msg = "Prayer month DELETED successfully";
			Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
		}
	}

	private void addPrayerMonth()
	{
		Spinner monthSpinner = (Spinner) getActivity().findViewById(R.id.aqp_month_spinner);
		Spinner yearSpinner = (Spinner) getActivity().findViewById(R.id.aqp_year_spinner);

		int month = (Integer) monthSpinner.getSelectedItemPosition();
		int year = (Integer) yearSpinner.getSelectedItem();

		PrayerDiaryDBHelper dbHelper = new PrayerDiaryDBHelper(getActivity());
		// Check if the month is already added
		if (dbHelper.isMonthAdded(month, year))
		{
			Utils u = new Utils();
			u.showMessage("Unsuccessful", "This month has already been added", getActivity()).run();
		}
		else
		{
			int days = calculateDays(month, year);

			Calendar cal = Calendar.getInstance();
			cal.clear();
			cal.set(Calendar.YEAR, year);
			cal.set(Calendar.MONTH, month);
			Date date = null;
			for (int i = 0; i < days; i++)
			{
				cal.set(Calendar.DAY_OF_MONTH, i + 1);
				date = cal.getTime();
				dbHelper.addCurrentDay(date);
			}
			CharSequence msg = "Prayer month ADDED successfully";
			Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
		}
	}

	private int calculateDays(int month, int year)
	{
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, 1); // 1st day of month
		Date startDate = cal.getTime();

		int nextMonth = (month == Calendar.DECEMBER) ? Calendar.JANUARY : month + 1;
		cal.set(Calendar.MONTH, nextMonth);
		if (month == Calendar.DECEMBER)
		{
			cal.set(Calendar.YEAR, year + 1);
		}
		Date endDate = cal.getTime();
		return (int) ((endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000));
	}
}